//
//  PatientMainHomeVC.m
//  Doctor
//
//  Created by 阳光 on 16/9/6.
//  Copyright © 2016年 阳光. All rights reserved.
//  病人管理主界面

#import "PatientMainHomeVC.h"
#import "FirstDocCell.h"
#import "VisitDocCell.h"

#import "VisitDocListModel.h"

#import "FirstPatientInfoVC.h"
#import "VisitPatientInfoVC.h"

#define selectViewH  45

static NSString *firstDocCellIdentifier=@"FirstDocCellIdentifier";
static NSString *visitDocCellIdentifier=@"VisitDocCellIdentifier";

@interface PatientMainHomeVC ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UILabel *downLine;

// 初诊
@property(nonatomic,strong)UITableView *firstDocTableView;
@property(nonatomic,strong)NSMutableArray *firstDocArr;

// 就诊
@property(nonatomic,strong)UITableView *visitDocTableView;

@property(nonatomic,strong)NSMutableArray *visitDocArr;


@end

@implementation PatientMainHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _firstDocArr=[NSMutableArray array];
    
    _visitDocArr=[NSMutableArray array];
    
    self.title = NSLocalizedString(@"My Patients", nil);//@"病人管理";
    
    [self initSelectView];
    
    [self initTableView];
    
    [self GetVisitPatientListData];
    
}


-(void)GetVisitPatientListData
{
    NSDictionary *dic=@{
                        @"doctor_id":[Util getUserDefaultObjectForKey:@"id"],
                        @"page":@"1",
                        @"count":@"20"
                        };
    
    __weak typeof(self) weakSelf=self;
    [QBRequest GetVisitPatientListWithDic:dic Success:^(id res)
    {
        weakSelf.visitDocArr=[VisitDocListModel mj_objectArrayWithKeyValuesArray:res[@"info"]];
        [weakSelf.visitDocTableView reloadData];
        [weakSelf.visitDocTableView.mj_header endRefreshing];
        
    } failure:^(NSString *error)
    {
        [weakSelf.visitDocTableView.mj_header endRefreshing];
    }];
}


-(void)initTableView
{
    // _firstDocTableView
    _firstDocTableView=[[UITableView alloc]initWithFrame:CGRectMake(0,selectViewH,APPW,APPH-selectViewH) style:UITableViewStylePlain];
    _firstDocTableView.delegate=self;
    _firstDocTableView.dataSource=self;
    _firstDocTableView.hidden=NO;
    _firstDocTableView.backgroundColor=MAIN_BACK_COLOR;
    _firstDocTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_firstDocTableView];
    _firstDocTableView.showsVerticalScrollIndicator=NO;
    [_firstDocTableView registerClass:[FirstDocCell class] forCellReuseIdentifier:firstDocCellIdentifier];
    
    // _firstDocTableView
    _visitDocTableView=[[UITableView alloc]initWithFrame:CGRectMake(0,selectViewH,APPW,APPH-selectViewH) style:UITableViewStylePlain];
    _visitDocTableView.delegate=self;
    _visitDocTableView.dataSource=self;
    _visitDocTableView.hidden=YES;
    _visitDocTableView.backgroundColor=MAIN_BACK_COLOR;
    _visitDocTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _visitDocTableView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_visitDocTableView];
    [_visitDocTableView registerClass:[VisitDocCell class] forCellReuseIdentifier:visitDocCellIdentifier];
    
}


-(void)initSelectView
{
    //标题
    UIView *titleView=[[UIView alloc]initWithFrame:CGRectMake(0,.5,APPW,selectViewH)];
    titleView.backgroundColor=WhiteColor;
    [self.view addSubview:titleView];
    
    
    
    NSArray *titleArr= @[NSLocalizedString(@"First visit", nil), NSLocalizedString(@"Treatment", nil)];//@[@"初诊",@"就诊"];
    for (int i =0;i<titleArr.count;i++) {
        UIButton *titleArrBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        titleArrBtn.frame = CGRectMake(APPW/2.0*i,2.5,APPW/2.0,40);
        [titleArrBtn setTitle:titleArr[i] forState:UIControlStateNormal];
        [titleArrBtn setTitleColor:Color333333 forState:UIControlStateNormal];
        [titleArrBtn setTitleColor:Main_Color forState:UIControlStateSelected];
        titleArrBtn.titleLabel.font=Font_Number(16);
        [titleArrBtn addTarget:self action:@selector(titleArrBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        titleArrBtn.tag=10+i;
        if (i == 0)
        {
            titleArrBtn.selected = YES;
        }
        [titleView addSubview:titleArrBtn];
    }
    
    // 中线
    UIView *midLine=[[UIView alloc]initWithFrame:CGRectMake(APPW/2.0-0.5,10,1.0,25)];
    midLine.backgroundColor=Color999999;
    [titleView addSubview:midLine];
    
    _downLine=[[UILabel alloc]initWithFrame:CGRectMake(50,42,APPW/2.0-100,2.0)];
    _downLine.backgroundColor=Main_Color;
    [titleView addSubview:_downLine];
    
}


-(void)titleArrBtnEvent:(UIButton *)sender
{
    for (int i = 0; i<2; i++)
    {
        UIButton *button = (UIButton *)[self.view viewWithTag:10+i];
        if (button.tag !=sender.tag)
        {
            button.selected=NO;
        }
        else
        {
            button.selected=YES;
        }
    }
    
    if (sender.tag==10)
    {
        _visitDocTableView.hidden=YES;
        _firstDocTableView.hidden=NO;
        _downLine.frame = CGRectMake(50,42,APPW/2.0-100,2);
    }
    else
    {
        _visitDocTableView.hidden=NO;
        _firstDocTableView.hidden=YES;
        _downLine.frame = CGRectMake(APPW/2.0+50,42,APPW/2.0-100,2);
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==_firstDocTableView)
    {
        return 1;
    }
    else
    {
        return self.visitDocArr.count;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==_firstDocTableView)
    {
        FirstDocCell *cell=[tableView dequeueReusableCellWithIdentifier:firstDocCellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
    }
    else
    {
        VisitDocCell *cell=[tableView dequeueReusableCellWithIdentifier:visitDocCellIdentifier];
        cell.visitDocListModel=self.visitDocArr[indexPath.row];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;
        
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==_firstDocTableView)
    {
        return 270/2;
    }
    else
    {
        return 220/2;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (tableView==_firstDocTableView)
    {
        // 初诊病人病情界面跳转
        FirstPatientInfoVC *firstPatientInfoVC=[FirstPatientInfoVC new];
        [self.navigationController pushViewController:firstPatientInfoVC animated:NO];
    }
    else
    {
        // 就诊病人病情界面跳转
        VisitPatientInfoVC *visitPatientInfoVC=[VisitPatientInfoVC new];
        VisitDocListModel *visitDocListModel=self.visitDocArr[indexPath.row];
        visitPatientInfoVC.visitDocListModel=self.visitDocArr[indexPath.row];
        visitPatientInfoVC.patientID=visitDocListModel.ID;
        [self.navigationController pushViewController:visitPatientInfoVC animated:NO];
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 64;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footView=[UIView new];
    return footView;
}


@end
